<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>新增教师</title>
    <link rel="stylesheet" href="../css/add_teacher.css">
    <script src="../js/jquery.js"></script>
    <script>
        $(document).ready(function () {
            // 页面加载时获取部门信息并填充到部门选择框
            loadDepartments();

            // 提交表单
            $("form").on("submit", function (e) {
                e.preventDefault(); // 阻止表单默认提交

                // 获取表单数据
                const teacherData = {
                    name: $("#name").val(),
                    idcard: $("#teacherid").val(),
                    gender: $("#gender").val(),
                    phone: $("#phone").val(),
                    address: $("#address").val(),
                    partymember: $('input[name="partymember"]:checked').val() === "是",
                    position: $("#position").val(),
                    departmentid: parseInt($("#department").val(), 10)
                };

                // 调用 /addTeacher 接口
                $.ajax({
                    url: "/addTeacher",
                    method: "POST",
                    contentType: "application/json",
                    data: JSON.stringify(teacherData),
                    success: function (response) {
                        alert("教师添加成功！");
                        window.location.href = "teacher.html"; // 跳转到教师列表页
                    },
                    error: function (xhr) {
                        alert(`教师添加失败：${xhr.responseText || "未知错误"}`);
                    }
                });
            });

            // 加载部门信息的函数
            function loadDepartments() {
                $.ajax({
                    url: "/queryDepartment", // 假设后端查询部门信息的接口路径，需按实际调整
                    method: "GET",
                    contentType: "application/json",
                    success: function (departments) {
                        const departmentSelect = $("#department");
                        // 清空原有选项（除了默认的提示选项）
                        departmentSelect.find('option:not(:first)').remove();
                        // 循环遍历部门数据，添加到选择框中
                        departments.forEach(function (department) {
                            departmentSelect.append(`<option value="${department.id}">${department.departname}</option>`);
                        });
                    },
                    error: function () {
                        alert("获取部门信息失败，请检查网络或服务端。");
                    }
                });
            }
        });
    </script>
</head>
<body>
<div class="container">
    <h1>新增教师</h1>
    <form>
        <label for="teacherid">身份证号：</label>
        <input type="text" id="teacherid" name="teacherid" placeholder="请输入身份证号" required>

        <label for="name">姓名：</label>
        <input type="text" id="name" name="name" placeholder="请输入姓名" required>

        <label for="gender">性别：</label>
        <input type="text" id="gender" name="gender" placeholder="请输入性别" required>

        <label for="phone">联系电话：</label>
        <input type="text" id="phone" name="phone" placeholder="请输入联系电话" required>

        <label for="address">地址：</label>
        <input type="text" id="address" name="address" placeholder="请输入地址" required>

        <label for="partymember">党员：</label>
        <div>
            <label>
                <input type="radio" id="partymember_yes" name="partymember" value="是" required> 是
            </label>
            <label>
                <input type="radio" id="partymember_no" name="partymember" value="否" required> 否
            </label>
        </div>

        <label for="position">职位：</label>
        <input type="text" id="position" name="position" placeholder="请输入职位" required>

        <label for="department">所在部门：</label>
        <select id="department" name="department" required>
            <option value="" disabled selected>请选择所在部门</option>
        </select>

        <button type="submit">提交</button>
        <button type="button" onclick="window.location.href='teacher.html'">返回</button>
    </form>
</div>
</body>
</html>